<?php
class BlogController extends \BaseController {

    public function getIndex() {
        return '博客首页'; //显示所有博客内容（包括最新博客和博主排行榜）
    }

    public function getUser($username) {
        return '个人博客';
    }

    public function getAdd() {

        //TODO
        return '';
    }

    public function postAdd() {

        if(!Auth::check()) {
            return array('error', '未登录');
        }

        $inputData = Input::all();

        $Blog = new Blog;
        $Blog->title = $inputData['title'];
        $Blog->body = $inputData['body'];
        $Blog->category_id = $inputData['category_id'];
        $Blog->user_id = Auto::user()->id;
        $Blog->tags = $inputData['tags'];

        $Blog->save();

        return '';
    }

    public function getEdit($id) {
        if(!Auth::check()) {
            return array('error', '未登录');
        }

        $Blog = Blog::find($id);

        if(!$Blog->id) {
            return array('error', '没有找到相应的记录');
        }

        if($Blog->user_id != Auth::user()->id) {
            return array('error', '没有权限');
        }

        return ''; //return edit page
    }

    public function postEdit() {
        if(!Auth::check()) {
            return array('error', '未登录');
        }

        $inputData = Input::all();

        $Blog = Blog::find($inputData->id);

        if(!$Blog->id) {
            return array('error', '没有找到相应的记录');
        }

        if($Blog->user_id != Auth::user()->id) {
            return array('error', '没有权限');
        }

        $Blog->title = $inputData['title'];
        $Blog->body = $inputData['body'];
        $Blog->category_id = $inputData['category_id'];
        $Blog->user_id = Auto::user()->id;
        $Blog->tags = $inputData['tags'];

        $Blog->save();

        return '';
    }

    public function getDelete($id) {
        if(!Auth::check()) {
            return array('error', '未登录');
        }


        $Blog = Blog::find($id);

        if(!$Blog->id) {
            return array('error', '没有找到相应的记录');
        }

        if($Blog->user_id != Auth::user()->id) {
            return array('error', '没有权限');
        }

        $Blog->delete();

        return '';

    }

    public function getView($id) {

        $Blog = Blog::find($id);

        $BlogComments = Comment::whereRaw('type = ? and belong_id = ?', array('blog', $Blog->id))->get();

        return array('blog' => $Blog, 'comments' => $BlogComments);
    }


    public function getSearch($keyword) {

        $blogs = Blog::whereRaw('name like ? or body like ? or tags like ?', array('%$keyword%', '%$keyword%', '%$keyword%'))->get();

        return $blogs;
    }

    public function postCategoryAdd() {
        if(!Auth::check()) {
            return array('error', '未登录');
        }

        $inputData = Input::all();

        $Category = new Category;
        $Category->name = $inputData['name'];
        $Category->user_id = Auth::user()->id;
        $Category->p_id = 2;
        $Category->save();

        return array('success' => '创建成功！');

    }

    public function getCategoryDelete($id) {
        if(!Auth::check()) {
            return array('error', '未登录');
        }

        $Category = Category::find($id);

        if($Category->user_id != Auth::user()->id) {
            return array('error', '没有权限');
        }

        $Category->delete();

        return array('success' => '删除成功！');

    }

    public function postCommentAdd() {
        if(!Auth::check()) {
            return array('error', '未登录');
        }

        $inputData = Input::all();

        $Comment = new Comment;

        $Comment->type = 'blog';
        $Comment->user_id = Auth::user()->id;
        $Comment->content = $inputData['content'];

        $Comment->save();

        return array('success', '添加成功！');
    }

    public function getCommentDelete($id) {
        if(!Auth::check()) {
            return array('error', '未登录');
        }

        $Comment = Comment::find($id);

        if($Comment->user_id != Auth::user()->id) {
            return array('error', '没有权限');
        }


        return array('success', '删除成功');

    }


}
